package com.lxp.tksys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.tksys.common.R;
import com.lxp.tksys.entity.Employee;
import com.lxp.tksys.service.EmployService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")  // 员工管理
public class EmployeeController {

    @Autowired
    private EmployService employService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //md5加密密码
        String password=employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        //username获取对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employService.getOne(queryWrapper);

        if(emp==null){
            return R.error("用户名不存在");
        }
        if(!emp.getPassword().equals(password)){  //emp与密码不同
            return R.error("密码错误");
        }
        if(emp.getStatus()==0){
            return R.error("账号已禁用");
        }
        //存入session
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    //退出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");    //移除session后通过拦截器实现拦截
        return R.success("退出成功");
    }

    //新增员工数据
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工{}",employee.toString());

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        //employee.setCreateTime(LocalDateTime.now());---以后都使用自动填充
        //employee.setUpdateTime(LocalDateTime.now());
        //获取登陆的人就是创造人
        //Long empId = (Long)request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        employService.save(employee);

        return R.success("新增员工成功");
    }

    //员工信息分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //log.info("page={},pageSize={},name={}",page,pageSize,name);

        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);     //根据name like模糊查询
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    //启用/禁用、编辑员工
    @PutMapping   //传来的id只有16位,JacksonObjectMapper中将long->string
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){

        //log.info("employee:{}",employee);

        long id=Thread.currentThread().getId();
        //log.info("线程id：{}",id);
        //employee.setUpdateTime(LocalDateTime.now());
        //Long empId = (Long)request.getSession().getAttribute("employee");
        //employee.setUpdateUser(empId);

        employService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    //根据id查询员工信息
    //前端使用String类型的ID,前端使用js string精度就不会丢失了。 那前端再把String类型的19位数字传回服务端的时候，可以用Long接收么？
    // 当然可以，这是Spring反序列化参数接收默认支持的行为。
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        //log.info("根据id查询员工信息");
        Employee employee = employService.getById(id);
        if(employee!=null)
            return R.success(employee);
        return R.error("没有查询到对应员工信息");
    }


}
